{% extends 'dashboard/base.html' %}

{% block title %}菜品管理 - 巧巧点餐管理后台{% endblock %}

{% block content %}
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-gray-800">菜品管理</h1>
    <a href="{% url 'dashboard:food_add' %}" class="btn btn-primary btn-sm">
        <i class="fas fa-plus fa-sm"></i> 添加菜品
    </a>
</div>

<div class="card shadow mb-4">
    <div class="card-header py-3">
        <h6 class="m-0 font-weight-bold text-primary">筛选条件</h6>
    </div>
    <div class="card-body">
        <form method="get" class="search-form">
            <div class="row">
                <div class="col-md-3 mb-2">
                    <div class="form-group mb-2">
                        <label for="search" class="small">搜索</label>
                        <input type="text" id="search" name="search" class="form-control" value="{{ request.GET.search|default:'' }}" placeholder="菜品名称...">
                    </div>
                </div>
                <div class="col-md-3 mb-2">
                    <div class="form-group mb-2">
                        <label for="category" class="small">分类</label>
                        <select id="category" name="category" class="form-control">
                            <option value="">全部分类</option>
                            {% for category in categories %}
                                <option value="{{ category.id }}" {% if request.GET.category == category.id|stringformat:"i" %}selected{% endif %}>{{ category.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
                <div class="col-md-2 mb-2">
                    <div class="form-group mb-2">
                        <label for="status" class="small">状态</label>
                        <select id="status" name="status" class="form-control">
                            <option value="">全部状态</option>
                            <option value="active" {% if request.GET.status == 'active' %}selected{% endif %}>上架</option>
                            <option value="inactive" {% if request.GET.status == 'inactive' %}selected{% endif %}>下架</option>
                        </select>
                    </div>
                </div>
                <div class="col-md-2 mb-2">
                    <div class="form-group mb-2">
                        <label class="small d-block">标签</label>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="checkbox" id="is_hot" name="is_hot" value="1" {% if request.GET.is_hot %}checked{% endif %}>
                            <label class="form-check-label small" for="is_hot">热销</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="checkbox" id="is_new" name="is_new" value="1" {% if request.GET.is_new %}checked{% endif %}>
                            <label class="form-check-label small" for="is_new">新品</label>
                        </div>
                    </div>
                </div>
                <div class="col-md-2 mb-2 d-flex align-items-end">
                    <div class="form-group mb-2 d-flex">
                        <button type="submit" class="btn btn-primary btn-sm mr-2">
                            <i class="fas fa-search fa-sm"></i> 搜索
                        </button>
                        <a href="{% url 'dashboard:food_list' %}" class="btn btn-light btn-sm">
                            <i class="fas fa-redo fa-sm"></i> 重置
                        </a>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
        
<!-- 菜品列表 -->
<div class="card shadow mb-4">
    <div class="card-header py-3">
        <h6 class="m-0 font-weight-bold text-primary">菜品列表</h6>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover" width="100%" cellspacing="0">
                <thead>
                    <tr>
                        <th width="8%">图片</th>
                        <th width="15%">名称</th>
                        <th width="12%">分类</th>
                        <th width="10%">价格（亲亲）</th>
                        <th width="10%">原价（亲亲）</th>
                        <th width="8%">销量</th>
                        <th width="15%">状态</th>
                        <th width="15%">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for food in foods %}
                    <tr>
                        <td class="text-center">
                            <img src="{{ food.get_image }}" alt="{{ food.name }}" class="img-thumbnail" style="width: 50px; height: 50px; object-fit: cover;">
                        </td>
                        <td class="text-truncate" style="max-width: 150px;">{{ food.name }}</td>
                        <td>{{ food.category.name }}</td>
                        <td>{{ food.price|floatformat:"0" }} 亲亲</td>
                        <td>{% if food.original_price %}{{ food.original_price|floatformat:"0" }} 亲亲{% else %}-{% endif %}</td>
                        <td>{{ food.sold }}</td>
                        <td>
                            {% if food.is_active %}
                            <span class="badge badge-success">已上架</span>
                            {% else %}
                            <span class="badge badge-danger">已下架</span>
                            {% endif %}
                            
                            {% if food.is_hot %}
                            <span class="badge badge-warning">热销</span>
                            {% endif %}
                            
                            {% if food.is_new %}
                            <span class="badge badge-info">新品</span>
                            {% endif %}
                        </td>
                        <td>
                            <div class="btn-group">
                                <a href="{% url 'dashboard:food_edit' food_id=food.id %}" class="btn btn-primary btn-sm">
                                    <i class="fas fa-edit fa-sm"></i> 编辑
                                </a>
                                <button type="button" class="btn btn-danger btn-sm delete-btn" 
                                        data-toggle="modal" data-target="#deleteModal" 
                                        data-id="{{ food.id }}" 
                                        data-name="{{ food.name }}">
                                    <i class="fas fa-trash fa-sm"></i> 删除
                                </button>
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="8" class="text-center py-4">暂无菜品数据</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteModalLabel">确认删除</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p>您确定要删除菜品 <strong id="foodName"></strong> 吗？</p>
                <p class="text-danger">此操作不可逆，删除菜品将同时删除与其相关的所有数据。</p>
            </div>
            <div class="modal-footer">
                <form id="deleteForm" method="post" action="">
                    {% csrf_token %}
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-danger">确认删除</button>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    $(document).ready(function() {
        // 删除确认
        $('#deleteModal').on('show.bs.modal', function (event) {
            const button = $(event.relatedTarget);
            const foodId = button.data('id');
            const foodName = button.data('name');
            
            $('#foodName').text(foodName);
            $('#deleteForm').attr('action', '/dashboard/foods/delete/' + foodId + '/');
        });
    });
</script>
{% endblock %} 